import os
from starlette.responses import StreamingResponse
from api.middleware import judge_token
from config.user import VIDEO_SAVE_PATH, IMAGE_SAVE_PATH, VIDEO_MIME_TYPES
from pathlib import Path
from config.comunity import IMAGE_SAVE_PATH as commnity_IMAGE_SAVE_PATH, TEXT_IMAGES_DIR
from fastapi import Depends, APIRouter

# 需要手动处理的文件。
files=APIRouter(prefix="/files",)
# 加载lifedata的资源文件

@files.get("/blog/life_data/send_file/{file_name}")
def get_mylife_data(file_name: str,type:str,userinfo=Depends(judge_token)):
    """
    用于渲染生活记录的图片/视频
    :param file_name:文件名
    :param type:video/image
    :return: stream
    """
    try:
        if type=='video':
            file_path = os.path.join(VIDEO_SAVE_PATH,file_name)
            extension=Path(file_name).suffix.lower()
            content_type=VIDEO_MIME_TYPES.get(extension, "application/octet-stream")
        else:
            file_path = os.path.join(IMAGE_SAVE_PATH, file_name)
            content_type='image/jpeg'

        if not os.path.exists(file_path):
            file_stream = open('public/yonghu.png', "rb")
            return StreamingResponse(file_stream, media_type='image/jpeg')
            pass
            # raise HTTPException(status_code=404, detail="File not found")

        # 下载文件 return FileResponse(file_path, media_type="application/octet-stream", filename=file_name)
        #流方式
        # media_type = "video/mp4" 指定这是个视频要播放不是下载
        file_stream= open(file_path, "rb")

        return StreamingResponse(file_stream, media_type=content_type,
                             headers={"Content-Disposition":  "inline; filename="+file_name})
    except Exception as e:
        print(e)

# 渲染社区首页
@files.get("/community/index/f/get/{file_name}")
async  def deal_index_rec_file(file_name:str ):
    """
    这是一个用于用户在社区获取渲染首页内容的
    :param file_name:
    :return:stream
    """
    file_path = os.path.join(commnity_IMAGE_SAVE_PATH, file_name)
    content_type='image/jpeg'
    if not os.path.exists(file_path):
        file_stream = open('public/yonghu.png', "rb")
        return StreamingResponse(file_stream, media_type='image/jpeg')
        pass
    else:
        file_stream = open(file_path, "rb")
        return StreamingResponse(file_stream, media_type=content_type,
                                 headers={"Content-Disposition": "inline; filename=" + file_name})

# 渲染用户设定的主页背景
@files.get("/blog/allbaby/f/get/{file_name}")
async def deal_baby_rec_file(file_name:str ,userinfo=Depends(judge_token)):
    """
    这是一个用户获取进入首页的子当以背景的。
    :param file_name:
    :return:
    """
    file_path = os.path.join(commnity_IMAGE_SAVE_PATH, file_name)
    content_type = 'image/jpeg'
    if not os.path.exists(file_path):
        file_stream = open('public/yonghu.png', "rb")
        return StreamingResponse(file_stream, media_type='image/jpeg')
        pass
    else:
        file_stream = open(file_path, "rb")
        return StreamingResponse(file_stream, media_type=content_type,
                                 headers={"Content-Disposition": "inline; filename=" + file_name})


@files.get("/community/text/image/get/{name}")
def deal_community_text_image(name:str):
    print(name)
    file_path = os.path.join(TEXT_IMAGES_DIR, name)
    print(file_path)
    content_type = 'image/jpeg'
    if not os.path.exists(file_path):
        file_stream = open('public/no_find.jpeg', "rb")
        return StreamingResponse(file_stream, media_type='image/jpeg')
        pass
    else:
        file_stream = open(file_path, "rb")
        return StreamingResponse(file_stream, media_type=content_type,
                                 headers={"Content-Disposition": "inline; filename=" + name})



